-- 睹机技能
local duji=fk.CreateSkill{
    name = "steam1__duji",

}
duji:addEffect("active",{
    anim_type = "control",
    prompt = "#steam1__duji",
    card_num = 0,
    target_num = 1,
    can_use = function(self, player)
      return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,
    target_filter = function(self,player, to_select, selected, selected_cards)
      return #selected == 0 and not to_select:isKongcheng()
    end,
    on_use = function(self, room, effect)
      local player = effect.from
      local target = effect.tos[1]
      local usecardname=function(cardnames,player,room,skillname,realcards)
        local names = player:getViewAsCardNames(skillname, cardnames)
        local card_name = room:askForChoice(player, names, skillname, "#steam__duji-choiceuse")
        local use=room:askToUseVirtualCard(player,
        {name=card_name,
        subcards=realcards,
        skill_name=skillname, 
        prompt="#steam__duji-use:::" .. card_name})
      end
      -- 展示手牌
      local handcards = target:getCardIds("h")
      target:showCards(handcards)
      
      
      -- 获取基本牌和非基本牌
      local basic_cards = table.filter(handcards, function(cid)
        return Fk:getCardById(cid).type == Card.TypeBasic
      end)
      local non_basic_cards = table.filter(handcards, function(cid)
        return Fk:getCardById(cid).type ~= Card.TypeBasic
      end)    -- 让目标选择选项
      local choices = {}
      if #basic_cards > 0 then
        table.insert(choices, "duji_basic")
      end
      if #non_basic_cards > 0 then
        table.insert(choices, "duji_non_basic")
      end
      
      if #choices == 0 then return end
      
      local choice = room:askForChoice(target, choices, self.name, "#steam__duji-choice")
      
      if choice == "duji_basic" then
        -- 将所有基本牌当一张基本牌使用
       
         local allCardIds = Fk:getAllCardIds()
         local allCardMapper = {}
         local allCardNames = {}
         for _, id in ipairs(allCardIds) do
           local card = Fk:getCardById(id)
           if card.type == Card.TypeBasic then
             if allCardMapper[card.name] == nil then
               table.insert(allCardNames, card.name)
             end
     
             allCardMapper[card.name] = allCardMapper[card.name] or {}
             table.insert(allCardMapper[card.name], id)
           end
         end
         usecardname(allCardNames,target,room,self.name,basic_cards)
  
     
      else
        -- 将所有非基本牌当一张普通锦囊牌使用
        local allCardIds = Fk:getAllCardIds()
        local allCardMapper = {}
        local allCardNames = {}
        for _, id in ipairs(allCardIds) do
          local card = Fk:getCardById(id)
          if card.type == Card.TypeTrick and card:isCommonTrick() then
            if allCardMapper[card.name] == nil then
              table.insert(allCardNames, card.name)
            end
    
            allCardMapper[card.name] = allCardMapper[card.name] or {}
            table.insert(allCardMapper[card.name], id)
          end
        end
  
        usecardname(allCardNames,target,room,self.name,non_basic_cards)
       
      end
  
      -- 如果目标不是自己，自己可以执行另一项
      if target ~= player then
        local phandcards = player:getCardIds("h")
        player:showCards(phandcards)
        
        -- 获取基本牌和非基本牌
        local pb = table.filter(phandcards, function(cid)
          return Fk:getCardById(cid).type == Card.TypeBasic
        end)
        local pnb = table.filter(phandcards, function(cid)
          return Fk:getCardById(cid).type ~= Card.TypeBasic
        end)
        if choice == "duji_basic" and #pb > 0 then
              -- 将所有非基本牌当一张普通锦囊牌使用
              local allCardIds = Fk:getAllCardIds()
              local allCardMapper = {}
              local allCardNames = {}
              for _, id in ipairs(allCardIds) do
                local card = Fk:getCardById(id)
                if card.type == Card.TypeTrick and card:isCommonTrick() then
                  if allCardMapper[card.name] == nil then
                    table.insert(allCardNames, card.name)
                  end
          
                  allCardMapper[card.name] = allCardMapper[card.name] or {}
                  table.insert(allCardMapper[card.name], id)
                end
              end
        
              usecardname(allCardNames,player,room,self.name,pnb)
        elseif choice == "duji_non_basic" and #pnb > 0 then
          local allCardIds = Fk:getAllCardIds()
          local allCardMapper = {}
          local allCardNames = {}
          for _, id in ipairs(allCardIds) do
            local card = Fk:getCardById(id)
            if card.type == Card.TypeBasic then
              if allCardMapper[card.name] == nil then
                table.insert(allCardNames, card.name)
              end
      
              allCardMapper[card.name] = allCardMapper[card.name] or {}
              table.insert(allCardMapper[card.name], id)
            end
          end
          usecardname(allCardNames,player,room,self.name,pb)
        end
      end
    end,
  })

return duji